/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.storage.core.pool;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.enfry.storage.core.pool.Pool;
import com.enfry.storage.core.pool.PoolAdapter;
import com.enfry.storage.core.pool.StorageClient;

/**
 * 连接池适配器抽象类
 * @Title com.enfry.storage.core.pool.PoolAdapter
 * @author liulei
 * @date 2016年7月25日
 */
public class PoolAdapter implements Pool {
    private final static Logger log = LoggerFactory.getLogger(PoolAdapter.class);
    private final GenericObjectPool internalPool;

    public PoolAdapter(Config poolConfig,
                       PoolableObjectFactory factory) {
        this.internalPool = new GenericObjectPool(factory, poolConfig);
    }

    public StorageClient getResource() throws Exception {
        return (StorageClient) this.internalPool.borrowObject();
    }

    public void returnResource(StorageClient resource) throws Exception {
        this.internalPool.returnObject(resource);
    }

    public void returnBrokenResource(StorageClient resource) throws Exception {
        this.internalPool.invalidateObject(resource);
    }

    public void destroy() throws Exception {
        this.internalPool.close();
    }
}
